package cn.szzsi.service;

import cn.szzsi.common.model.DeliveryAreaRelation;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DeliveryAreaRelationService {
    public static final DeliveryAreaRelationService service=new DeliveryAreaRelationService();


    /**
     *
     * @param regions 地区id集合
     * @param deliveryTypeId 配送方式id
     * @param shopId
     */
    public void update(Integer []regions,String deliveryTypeId,String shopId){
        List<DeliveryAreaRelation> toUpdate=new ArrayList<>();
        for(int i=0;i<regions.length;i++){
            DeliveryAreaRelation deliveryAreaRelation=new DeliveryAreaRelation();
            deliveryAreaRelation.setId(StrKit.getRandomUUID());
            deliveryAreaRelation.setAreaId(regions[i]);
            deliveryAreaRelation.setDeliveryTypeId(deliveryTypeId);
            deliveryAreaRelation.setShopId(shopId);
            deliveryAreaRelation.setUpdateDate(new Date());

            toUpdate.add(deliveryAreaRelation);
        }
        if(toUpdate.size()>0){
            //在此做全量替换,即把原该影院的排期全部删除,重新插入新的
            Db.update("delete from mall_delivery_area_relation where deliveryTypeId=?",deliveryTypeId);
            Db.batchSave(toUpdate, 1000);
        }
    }

    /**
     *
     * @param deliveryTypeId 配送方式id
     */
    public List<Record> getByDeliveryTypeId(String deliveryTypeId){
        List<Record> recordList=Db.find("select d.areaId,a.`name` from `mall_delivery_area_relation` d LEFT JOIN mall_area_system a on d.areaId=a.id where d.deliveryTypeId=? order by d.areaId asc",deliveryTypeId);
        return recordList;
    }
}
